package com.example.service.impl;

import com.example.domain.UrlResource;
import com.example.mapper.UrlResourceMapper;
import com.example.service.DynamicSecurityService;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class DynamicSecurityServiceImpl implements DynamicSecurityService {
    @Resource
    private UrlResourceMapper urlResourceMapper;

    @Override
    public Map<String, ConfigAttribute> loadDataSource() {
        Map<String, ConfigAttribute> map = new ConcurrentHashMap<>();
        List<UrlResource> resourceList = urlResourceMapper.getResourceList();
        for (UrlResource resource : resourceList) {
            map.put(resource.getUrl(), new SecurityConfig(resource.getId() + ":" + resource.getName()));
        }
        return map;
    }
}
